﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using PatientInformationExtract.Components;
using PatientInformationExtract.Dictionary.ViewModels;

namespace PatientInformationExtract.Dictionary.Views
{
    /// <summary>
    /// Interaction logic for TranslationLearningDictionaryManageWindow.xaml
    /// </summary>
    public partial class TranslationLearningDictionaryManageWindow : Window
    {
        private WindowViewModelBase _viewModel;

        public TranslationLearningDictionaryManageWindow(WindowViewModelBase viewModel)
        {
            InitializeComponent();
            this._viewModel = viewModel;
        }

        private void miChineseAddToDicitonary_Click(object sender, RoutedEventArgs e)
        {
            string word = ((((sender as System.Windows.Controls.MenuItem).Parent as ContextMenu).Parent as System.Windows.Controls.Primitives.Popup).PlacementTarget as TextBlock).Text;
            string translation = "";
            Singleton<MainInterface.Models.MainInterfaceModel>.Instance.TranslationLearningDict.TryGetTranslation(word, out translation);
            ((this._viewModel as TranslationLearningDictionaryManageViewModel).Parent as DictionaryManageViewModel).AddNewWordOutSide(word, translation.Replace("*", ""));
        }

        private void miAddSegment_Click(object sender, RoutedEventArgs e)
        {
            ((this._viewModel as TranslationLearningDictionaryManageViewModel).Parent as DictionaryManageViewModel).AddNewWordOutSide((sender as MenuItem).Header.ToString(), "");
        }

        private void miUnknownSegmentsAddToDicitonary_Loaded(object sender, RoutedEventArgs e)
        {
            string word = ((((sender as System.Windows.Controls.MenuItem).Parent as ContextMenu).Parent as System.Windows.Controls.Primitives.Popup).PlacementTarget as TextBlock).Text;
            MenuItem miUnknownSegmentsAddToDicitonary = (MenuItem)sender;
            miUnknownSegmentsAddToDicitonary.Items.Clear();
            foreach (string segment in word.Split(new char[] { ',' }))
            {
                MenuItem mi = new MenuItem();
                mi.Header = segment;
                miUnknownSegmentsAddToDicitonary.Items.Add(mi);
                mi.Click += new RoutedEventHandler(miAddSegment_Click);
            }
        }

        private void miDeleteRow_Click(object sender, RoutedEventArgs e)
        {
            string word = ((((sender as System.Windows.Controls.MenuItem).Parent as ContextMenu).Parent as System.Windows.Controls.Primitives.Popup).PlacementTarget as TextBlock).Text;
            if (MessageBox.Show("Do you want to delete this row ?", "Delete", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
            {
                (this._viewModel as TranslationLearningDictionaryManageViewModel).DeleteRecordByChinese(word);
            }
        }
    }
}
